cmake_minimum_required(VERSION 3.10)

project(CPP_POC)

set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

set(CMAKE_CXX_STANDARD 20)

include(ExternalProject)

set(build_output ${CMAKE_CURRENT_BINARY_DIR}/output)

ExternalProject_Add(external_boost
    URL /home/zuoxf/pkgs/boost_1_88_0.tar.bz2
    # URL https://archives.boost.io/release/1.88.0/source/boost_1_88_0.tar.bz2
    URL_HASH SHA256=46d9d2c06637b219270877c9e16155cbd015b6dc84349af064c088e9b5b12f7b
    DOWNLOAD_EXTRACT_TIMESTAMP True
    CONFIGURE_COMMAND ./bootstrap.sh
    BUILD_IN_SOURCE True
    BUILD_COMMAND ./b2 cxxstd=20 --prefix=${build_output} --with-log --with-program_options --with-serialization --with-test --with-uuid --with-url --with-unordered --with-tokenizer --with-ptr_container --with-lexical_cast --with-json --with-interprocess --with-cobalt --with-beast --with-asio install
    INSTALL_COMMAND echo "boost installed."
)

file(MAKE_DIRECTORY ${build_output}/lib)
file(CREATE_LINK ${build_output}/lib ${build_output}/lib64 SYMBOLIC)

ExternalProject_Add(external_openssl
    URL /home/zuoxf/pkgs/openssl-3.5.1.tar.gz
    # URL https://github.com/openssl/openssl/releases/download/openssl-3.5.1/openssl-3.5.1.tar.gz
    URL_HASH SHA256=529043b15cffa5f36077a4d0af83f3de399807181d607441d734196d889b641f
    DOWNLOAD_EXTRACT_TIMESTAMP True
    CONFIGURE_COMMAND ./Configure --prefix=${build_output} --openssldir=${build_output}/ssl
    BUILD_IN_SOURCE True
    BUILD_COMMAND make -j 8
    INSTALL_COMMAND make install
)

ExternalProject_Add(external_zlib
    URL /home/zuoxf/pkgs/zlib-1.3.1.tar.gz
    # URL https://www.zlib.net/zlib-1.3.1.tar.gz
    DOWNLOAD_EXTRACT_TIMESTAMP True
    CONFIGURE_COMMAND cmake <SOURCE_DIR> -G Ninja --install-prefix ${build_output}
    BUILD_COMMAND cmake --build ./ -j 8
    INSTALL_COMMAND cmake --install ./ --prefix ${build_output}
)

ExternalProject_Add(external_libssh2
    URL /home/zuoxf/pkgs/libssh2-1.11.1.tar.bz2
    # URL https://libssh2.org/download/libssh2-1.11.1.tar.bz2
    DEPENDS external_openssl
    DOWNLOAD_EXTRACT_TIMESTAMP True
    CONFIGURE_COMMAND cmake <SOURCE_DIR> -G Ninja --install-prefix ${build_output}
    BUILD_COMMAND cmake --build ./ -j 8
    INSTALL_COMMAND cmake --install ./ --prefix ${build_output}
)

ExternalProject_Add(external_curl
    DEPENDS external_openssl external_zlib external_libssh2
    URL /home/zuoxf/pkgs/curl-8.14.1.tar.bz2
    # URL https://curl.se/download/curl-8.14.1.tar.bz2
    DOWNLOAD_EXTRACT_TIMESTAMP True
    BUILD_IN_SOURCE True
    CONFIGURE_COMMAND ${CMAKE_COMMAND} -E env LDFLAGS=-L${build_output}/lib\ -Wl,-z,relro,-z,now,-rpath='\$\$ORIGIN' ./configure --disable-ldap --disable-ldaps --disable-pop3 --disable-imap --disable-smtp --with-zlib=${build_output} --with-ssl=${build_output} --with-libssh2=${build_output} --prefix=${build_output} --enable-ipv6 --enable-websockets --without-ca-path --without-ca-bundle --without-libpsl
    BUILD_COMMAND make -j 16
    INSTALL_COMMAND make install
)

include_directories(
    ${CMAKE_CURRENT_BINARY_DIR}/output/include
)

link_directories(
    ${CMAKE_CURRENT_BINARY_DIR}/output/lib
)

add_subdirectory(async)
add_subdirectory(boost_program_options)
